package com.bootdo.friendcircle.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.friendcircle.domain.FriendCircleCommentDO;
import com.bootdo.friendcircle.service.FriendCircleCommentService;
import com.bootdo.system.service.UserService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 
 * 
 * @author lzy
 * @email 599048244@qq.com
 * @date 2018-06-09 09:29:21
 */
 
@Controller
@RequestMapping("/friendcircle/friendCircleComment")
public class FriendCircleCommentController {
	@Autowired
	private FriendCircleCommentService friendCircleCommentService;
	@Autowired
	private UserService userService;
	
	@GetMapping()
	@RequiresPermissions("friendcircle:friendCircleComment:friendCircleComment")
	String FriendCircleComment(){
	    return "friendcircle/friendCircleComment/friendCircleComment";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("friendcircle:friendCircleComment:friendCircleComment")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<FriendCircleCommentDO> friendCircleCommentList = friendCircleCommentService.list(query);
		int total = friendCircleCommentService.count(query);
		PageUtils pageUtils = new PageUtils(friendCircleCommentList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("friendcircle:friendCircleComment:add")
	String add(){
	    return "friendcircle/friendCircleComment/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("friendcircle:friendCircleComment:edit")
	String edit(@PathVariable("id") Integer id,Model model){
		FriendCircleCommentDO friendCircleComment = friendCircleCommentService.get(id);
		model.addAttribute("friendCircleComment", friendCircleComment);
	    return "friendcircle/friendCircleComment/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save( FriendCircleCommentDO friendCircleComment){
		Long userId = friendCircleComment.getUserId();
		String companyName = userService.get(userId).getCompanyName();
		friendCircleComment.setUserName(companyName);
		if(friendCircleCommentService.save(friendCircleComment)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("friendcircle:friendCircleComment:edit")
	public R update( FriendCircleCommentDO friendCircleComment){
		friendCircleCommentService.update(friendCircleComment);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("friendcircle:friendCircleComment:remove")
	public R remove( Integer id){
		if(friendCircleCommentService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("friendcircle:friendCircleComment:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] ids){
		friendCircleCommentService.batchRemove(ids);
		return R.ok();
	}
	
}
